﻿using System;
using System.Collections.Generic;

using System.Text;

namespace RebEngine.GameComponents
{
    public class EventCounter
    {
        static List<EventCounter> Timers = new List<EventCounter>();

        private int iCountRemaining;
        private int iInterval;
        private int GUID;
        private bool bContinousTimer;

        public delegate void Fire(int ID);
        public event Fire TickTimerFinished;

        public void Start(int ID,int interval, bool continuous)
        {
            Timers.Remove(this);
            Timers.Add(this);
            this.iInterval = interval;
            this.iCountRemaining = interval;
            bContinousTimer = continuous;
            GUID = ID;
        }

        public void Stop()
        {
            Timers.Remove(this);
        }

        public static void Tick(int amountDone)
        {
            for (int i = Timers.Count - 1; i >= 0; i--)
            {
                Timers[i].iCountRemaining -= amountDone;
                if (Timers[i].iCountRemaining <= 0)
                {
                    Timers[i].TickTimerFinished(Timers[i].GUID);
                    if (Timers[i].bContinousTimer == false)
                        Timers[i].Stop();
                    else
                        Timers[i].iCountRemaining += Timers[i].iInterval; // This allows overflow research or build production. Requires testing
                }
            }
        }

    }
}
